/*
	Custom part for ForceDirected
*/

  function parseForceDirected(jsonArray, docId, forcedirected) { 
	
	var str = '{ "_id": ' + format(docId) + ',';
	str += ' "json": [ ';
	
	var arrayLength = jsonArray.length;
	for (var i = 0; i < arrayLength; i++) {
		str += " { ";
		
		var nodeJsonId = jsonArray[i].id;
		str += ' "id": ';
		
		var node = forcedirected.graph.getNode(nodeJsonId);		
		if(null == nodeJsonId)
		{
			nodeJsonId = "node" + Math.random();				
		}
		str += format(nodeJsonId) + ",";
		
		// getNode 
		var posNode = node.getPos("current").getc(true);
		var posNodeX = posNode.x;
		var posNodeY = posNode.y;
		
		var jsonName = jsonArray[i].name; 
		str += ' "name": ';
		str += format(jsonName) + ",";
		
		str += ' "data": { ';
		var jsonDataColor = jsonArray[i].data.$color;
		// offset "$color": "#EBB056",
		str += ' "$color": ';
		str += format(jsonDataColor) + ",";
		// str += format("#EBB056") + ",";
		
		var jsonDataType = jsonArray[i].data.$type; //		
        // offset "$type": "circle",
		str += ' "$type": ';
		str += format(jsonDataType) + ",";
		// str += format("circle") + ",";	
		
		if(null != jsonArray[i].data.ospfRouterId)
		{
			str += ' "ospfRouterId": '; 
			str += format(jsonArray[i].data.ospfRouterId) + ",";
		}
		
		if(null != jsonArray[i].data.database)
		{
			str += ' "database": '; 
			str += format(jsonArray[i].data.database) + ",";
		}
									
		str += ' "posX": '; 
		str += format(posNodeX) + ",";
		 
		str += ' "posY": '; 
		str += format(posNodeY) + "},";

		// var jsonIpaddress = jsonArray[i].data.ipAddress; //
		// str += ' "ipAddress": ';
		// str += format(jsonIpaddress) + ",";
		
		// var jsonInterfaceFa = jsonArray[i].data.interfaceFa; //
		// str += ' "interfaceFa": ';
		// str += format(jsonInterfaceFa) + "},";
		
		var jsonAdjacencies = jsonArray[i].adjacencies; // array
		str += ' "adjacencies": [ ';
		
		var adjacenciesLength = jsonAdjacencies.length;
		for (var j = 0; j < adjacenciesLength; j++) {
		
			str += " { ";
			
			str += ' "nodeTo": ';
			if(nodeJsonId == jsonAdjacencies[j].data.nodeTo){
				str += format(jsonAdjacencies[j].data.nodeFrom) + ",";
			}
			else {
				str += format(jsonAdjacencies[j].data.nodeTo) + ",";
			}
			
			str += ' "nodeFrom": ';	
			if(nodeJsonId == jsonAdjacencies[j].data.nodeTo){
				str += format(jsonAdjacencies[j].data.nodeTo) + ",";
			}
			else {
				str += format(jsonAdjacencies[j].data.nodeFrom) + ",";
			} 
			
			str += ' "data": {';			
			str += ' "id": ';			
			var jsonAdjacencyDataId = jsonAdjacencies[j].data.id;
			if(null == jsonAdjacencyDataId)
			{
				jsonAdjacencyDataId = "adj" + Math.random();				
			}
			str += format(jsonAdjacencyDataId);
			str += ' , ';
			
			if(null != jsonAdjacencies[j].data.bandwidth)
			{
				str += ' "bandwidth": ';
				str += format(jsonAdjacencies[j].data.bandwidth);			
				str += ' , ';
			}

			if(null != jsonAdjacencies[j].data.ospfstate)
			{
				str += ' "ospfstate": ';
				str += format(jsonAdjacencies[j].data.ospfstate);			
				str += ' , ';
			}
			if(null != jsonAdjacencies[j].data.type)
			{
				str += ' "type": ';
				str += format(jsonAdjacencies[j].data.type);			
				str += ' , ';
			}
			if(null != jsonAdjacencies[j].data.$color)
			{
				str += ' "$color": ';
				str += format(jsonAdjacencies[j].data.$color);		
				str += ' , ';
			}	
			if(null != jsonAdjacencies[j].data.$labeltext)
			{
				str += ' "$labeltext": ';
				str += format(jsonAdjacencies[j].data.$labeltext);			
				str += ' , ';
			}
			/////////////////////////////////////////////////////////////////////:
			
			str += ' "nodeFromInterface": ';
			if(nodeJsonId == jsonAdjacencies[j].data.nodeTo){
				str += format(jsonAdjacencies[j].data.nodeToInterface);
			} else {
				str += format(jsonAdjacencies[j].data.nodeFromInterface);
			}
			str += ' , ';
			
			str += ' "nodeFromIpAddress": ';
			if(nodeJsonId == jsonAdjacencies[j].data.nodeTo){
				str += format(jsonAdjacencies[j].data.nodeToIpAddress);
			} else {
				str += format(jsonAdjacencies[j].data.nodeFromIpAddress);
			}
			str += ' , ';
			
			str += ' "nodeFromMask": ';
			if(nodeJsonId == jsonAdjacencies[j].data.nodeTo){
				str += format(jsonAdjacencies[j].data.nodeToMask);
			} else {
				str += format(jsonAdjacencies[j].data.nodeFromMask);
			}
			str += ' , ';
			
			str += ' "nodeFrom": ';
			if(nodeJsonId == jsonAdjacencies[j].data.nodeTo){
				str += format(jsonAdjacencies[j].data.nodeTo) + ",";
			} else {
				str += format(jsonAdjacencies[j].data.nodeFrom) + ",";
			}
			/////////////////////////////////////////////////////////////////////:
			
			str += ' "nodeToInterface": ';
			if(nodeJsonId == jsonAdjacencies[j].data.nodeTo){
				str += format(jsonAdjacencies[j].data.nodeFromInterface);
			} else {
				str += format(jsonAdjacencies[j].data.nodeToInterface);
			}
			str += ' , ';
			
			str += ' "nodeToIpAddress": ';
			if(nodeJsonId == jsonAdjacencies[j].data.nodeTo){
				str += format(jsonAdjacencies[j].data.nodeFromIpAddress);
			} else {
				str += format(jsonAdjacencies[j].data.nodeToIpAddress);
			}
			str += ' , ';

			str += ' "nodeToMask": ';
			if(nodeJsonId == jsonAdjacencies[j].data.nodeTo){
				str += format(jsonAdjacencies[j].data.nodeFromMask);
			} else {
				str += format(jsonAdjacencies[j].data.nodeToMask);
			}
			str += ' , ';
			
			str += ' "nodeTo": ';
			if(nodeJsonId == jsonAdjacencies[j].data.nodeTo){
				str += format(jsonAdjacencies[j].data.nodeFrom);
			} else {
				str += format(jsonAdjacencies[j].data.nodeTo);
			}
			
			str += ' } ';
			str += " },";
		}
		str = str.substring(0, str.length - 1);
		
		str += ' ] },';
 	}
	str = str.substring(0, str.length - 1);
	
	str += " ]}";
	
	return str;
 }
 function format(elem) { 
	return '"'+elem+'"';
 }